{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "01a42855",
   "metadata": {},
   "source": [
    "### 2.3.4字符串格式化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed88d431",
   "metadata": {},
   "source": [
    "#### 1.占位符%"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "23d913c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "welcome zhang san\n"
     ]
    }
   ],
   "source": [
    "name = \"zhang san\"\n",
    "s1 = \"Welcome %s\"\n",
    "print(s1 % (name))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "557acdf1",
   "metadata": {},
   "source": [
    "#### 2.format函数\n",
    "* Python2.6 开始，新增函数 str.format()，增强格式化功能。\n",
    "* 基本语法是通过 {} 和 : 来代替以前的 % 。\n",
    "* format 函数可以接受任意多个参数，位置可以不按顺序。\n",
    "* 字符串的参数使用{NUM}进行表示, 0表示第一个参数, 1表示第二个参数, 以后顺次递加;\n",
    "* 使用“:”, 指定代表元素需要的操作, 如“:.3”有效数字三位（并非小数点后三位）, \":8\"占8个字符空间等;\n",
    "* 数字(0, 1, ...)即代表format()里面的元素, 所以可以使用“.”调用该元素的方法;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "584bf311",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome zhang san\n"
     ]
    }
   ],
   "source": [
    "name = \"zhang san\"\n",
    "welcome = \"Welcome {}\"\n",
    "print(welcome.format(name))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "571e8515",
   "metadata": {},
   "source": [
    "#### 3.使用f-string（Python 3.6及以上版本）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e611095a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome zhang san\n",
      "Welcome zhang sanzhang sanzhang san \n"
     ]
    }
   ],
   "source": [
    "name = \"zhang san\"\n",
    "print(f\"Welcome {name}\")\n",
    "print(f\"Welcome {name*3}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0351c585",
   "metadata": {},
   "source": [
    "### 2.3.5字符串的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8571d93d",
   "metadata": {},
   "source": [
    "#### 分割与组合\n",
    "* split([sep,[maxsplit]]):以sep为分隔符，分割成list，maxsplit表示分割次数，从左开始\n",
    "* rsplit([sep,[maxsplit]]):同上，但分割从右开始\n",
    "* splitlines([keepends]):以行分隔符分割成list，keepends取bool值，如果为真，每行分隔符会保留\n",
    "* join(seq): 把seq中元素串接成\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "c2307634",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b.c.d.jpg']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = \"https://www.asd.sfe.cm/sf/dfe\"\n",
    "url.split('/')\n",
    "\n",
    "filename = \"test.jpg\"\n",
    "filename.split('.')\n",
    "\n",
    "f = 'a.b.c.d.jpg'\n",
    "f.split('.',1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8608ef3",
   "metadata": {},
   "source": [
    "#### 查找\n",
    "* find(substr, [start, [end]]) ：返回字符串中出现substr的第一个字母的下标 ，没有substr则返回-1 ，start和end作用就相当于在 [start:end]中搜索 \n",
    "* index(substr, [start, [end]]) ：与find()相同，只是在字符串中没有substr时，会返回一个运行时错误 \n",
    "* rfind(substr, [start, [end]])：从右边开始搜索\n",
    "* rindex(substr, [start, [end]]) ：从右边开始搜索\n",
    "* count(substr, [start, [end]]) ：计算substr在字符串中出现的次数 \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "29f90a31",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = 'a.b.c.d.jpg'\n",
    "f.find(\".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f0b204e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f = 'a.b.c.d.jpg'\n",
    "f.rfind(\".\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "146aacf4",
   "metadata": {},
   "source": [
    "#### 替换\n",
    "* replace(oldstr, newstr, [count]) ：把字符串中oldstar替换为newstr，count为替换次数。这是替换的通用形式，还有一些函数进行特殊字符的替换\n",
    "* strip([chars])：把字符串中前后chars有的字符全部去掉，可以理解为把字符串前后chars替换为None \n",
    "* lstrip([chars]) ：同上，去掉左边\n",
    "* rstrip([chars]) ：同上，去掉右边\n",
    "* expandtabs([tabsize])：把字符串中的tab字符替换为空格，每个tab替换为tabsize\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "348b23ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello'"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = '6789hello9867'\n",
    "s.strip('6789')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d76f981",
   "metadata": {},
   "source": [
    "### 字符串性质\n",
    "* 字符串是一种序列\n",
    "* 字符串是不可变的\n",
    "* 修改字符串后就会重新产生一个新字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8cae6985",
   "metadata": {},
   "source": [
    "### 用print控制格式化输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e7575498",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.14\n"
     ]
    }
   ],
   "source": [
    "num=3.1415\n",
    "print(f\"{num:.2f}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d0991a56",
   "metadata": {},
   "source": [
    "### 数组的长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1d30bba6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "x = [0, -1, 0, None, (), [5,8,10]]\n",
    "print(len(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64b2563e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
